package com.sakiko.dailynote.controller;

import com.sakiko.dailynote.pojo.Result;
import com.sakiko.dailynote.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {

//    @PostMapping("/upload")
//    public Result<String> upload(MultipartFile file) throws Exception {
//        // 把文件内容存储到本地磁盘上
//        String originalFilename = file.getOriginalFilename();
//        // 保证文件的名字是唯一的，从而防止文件覆盖
//        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
////        file.transferTo(new File("C:\\Users\\lenovo\\Desktop\\big_event_files\\"+filename));
//        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
////        return Result.success("url访问地址...");
//        return Result.success(url);
//    }

    // minio
    @Autowired
    MinioUtil minioUtil;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {

        // 保证文件的名字是唯一的，从而防止文件覆盖
        String filename = UUID.randomUUID().toString().replaceAll("-","") + "-" +file.getOriginalFilename();

        // 文件上传
        String url = minioUtil.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }
}
